#include <iostream>
#include <cstring>

using namespace std;

class Test
{
public:
    Test()
    {
        m_p = nullptr;
    }

    Test(const char *ptr)
    {
        int len = strlen(ptr);
        m_p = new char[len + 1];
        strcpy(m_p, ptr);

        cout << "Test" << endl;
    }
    
    virtual void printMsg()
    {
        cout << "Test = " << m_p << endl;
    }

    virtual ~Test()
    {
        if(m_p != nullptr)
        {
            delete [] m_p;
        }

        m_p = nullptr;

        cout << "~Test" << endl;
    }
    char *m_p;
};

class TestA : public Test
{
public:
    
    TestA(const char *ptr) : Test(ptr)
    {
        int len = strlen(ptr);
        m_pa = new char[len + 1];
        strcpy(m_pa, ptr);

        
        cout << "TestA" << endl;
    }

    void printMsg() override
    {

    }

    char *m_pa;

    ~TestA()
    {
        if(m_pa != nullptr)
        {
            delete [] m_pa;
        }

        m_pa = nullptr;
        
        cout << "~TestA" << endl;
    }
};


int main(int argc, char **argv)
{
    Test *pt = new TestA("hello world");
    /**
     * ！！！
     * 一定要将基类的析构函数声明为虚函数：因为通过基类指针释放派生类对象的空间，要调用派生类
     * 的析沟函数，所以析构函数必须为虚函数；
     */
    delete pt;
    
    return 0;
}